package net.lab1024.sa.user.module.business.course.service;

import java.util.List;

import net.lab1024.sa.common.common.util.SmartRequestUtil;
import net.lab1024.sa.user.module.business.course.dao.StudentDao;
import net.lab1024.sa.user.module.business.course.domain.entity.StudentEntity;
import net.lab1024.sa.user.module.business.course.domain.form.StudentAddForm;
import net.lab1024.sa.user.module.business.course.domain.form.StudentQueryForm;
import net.lab1024.sa.user.module.business.course.domain.form.StudentUpdateForm;
import net.lab1024.sa.user.module.business.course.domain.vo.StudentVO;
import net.lab1024.sa.common.common.util.SmartBeanUtil;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学员 Service
 *
 * @Author jabari
 * @Date 2023-08-05 17:22:36
 * @Copyright 51dev.cn
 */

@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<StudentVO> queryPage(StudentQueryForm queryForm) {
        queryForm.setUserId(SmartRequestUtil.getRequestUserId());
        queryForm.setDeletedFlag(false);
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<StudentVO> list = studentDao.queryPage(page, queryForm);
        PageResult<StudentVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(StudentAddForm addForm) {
        StudentEntity studentEntity = SmartBeanUtil.copy(addForm, StudentEntity.class);
        studentEntity.setUserId(SmartRequestUtil.getRequestUserId());
        studentDao.insert(studentEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(StudentUpdateForm updateForm) {
        StudentEntity studentEntity = SmartBeanUtil.copy(updateForm, StudentEntity.class);
        studentDao.updateById(studentEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        studentDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer studentId) {
        if (null == studentId){
            return ResponseDTO.ok();
        }

        studentDao.updateDeleted(studentId,true);
        return ResponseDTO.ok();
    }
}
